package api.google.android;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PopupTemplate extends Dialog{
	
	private Dialog dialog = this;
	private String name = "";
	private String labelText = "";
	private String OkButtonValue = "";
	private Context context = null;
	private Connection connection = null;
	private MainMenu mainMenu = null;
	
    public interface ReadyListener {
        public void ready(String name);
    }


    public PopupTemplate(Context context, String name, String label, String OkButtonValue, Connection connection, MainMenu mainMenu) {
        super(context);
        this.context = context;
        this.name = name;
        this.labelText = label;
        this.OkButtonValue = OkButtonValue;
        this.connection = connection;
        this.mainMenu = mainMenu;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_new_group_dialog);
        
        TextView label = (TextView)findViewById(R.id.label);
        label.setText("" + labelText);
        
        setTitle(name);
                 
        Button buttonOK = (Button) findViewById(R.id.test);
        buttonOK.setText(OkButtonValue);
        buttonOK.setOnClickListener(new OKListener());
        
        Button buttonCancel = (Button) findViewById(R.id.cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				dialog.dismiss();
			}    	
        });
    }

    public class OKListener implements android.view.View.OnClickListener {
        public void onClick(View v) {
        	String groupName   = ((EditText)findViewById(R.id.textfield)).getText().toString();
        	String securityKey = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
        	connection.addPostData("name", groupName);
            connection.addPostData("securityKey", securityKey);
        	String response = connection.postData();
        	Toast.makeText( context, response, Toast.LENGTH_SHORT ).show();
        	dialog.dismiss();
        	if(response.contains("succesfully") && (connection.getPostData(0) == "join"  || connection.getPostData(0) == "create")){
        		mainMenu.UpdateSpinner();
        	}
        	
        }
    }    
}

